//
//  WBEmoticonKeyBoardView.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/18.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

class WBEmoticonKeyBoardView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    // MARK: - 设置UI
    func setupUI() {
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        //添加控件
        addSubview(bottomView)
        addSubview(pageView)
        addSubview(pageControl)
        //设置pageControl
        self.setupPageControl(NSIndexPath(forItem: 0, inSection: 0))
        
        //设置约束
        bottomView.snp_makeConstraints { (make) in
            
            make.leading.trailing.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        pageView.snp_makeConstraints { (make) in
            make.leading.trailing.top.equalTo(self)
            make.bottom.equalTo(bottomView.snp_top)
        }
        
        pageControl.snp_makeConstraints { (make) in
            
            make.bottom.centerX.equalTo(pageView).offset(5)
            
        }
        
        bottomView.closure = { (type:WBEmoticonBottomViewType) -> () in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: type.rawValue)
            
            self.pageView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
            //设置pageControl
            self.setupPageControl(indexPath)
        }
        
        
        
    }
    // MARK: - 懒加载pageControl
    private lazy var pageControl: UIPageControl = {
    
        let control = UIPageControl()
        
        //设置不可交互
        control.enabled = false
        //私有属性 通过KVC设置
        control.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "pageImage")
        control.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "currentPageImage")
    
        return control
    }()
    
    // MARK: - 懒加载底部视图
    private lazy var bottomView: WBEmoticonBottomView = WBEmoticonBottomView(frame: CGRectZero)
    // MARK: - 懒加载分页视图
    private lazy var pageView: WBEmoticonPageView = {
        
        let view = WBEmoticonPageView()
        
        view.backgroundColor = self.backgroundColor
        
        //设置分页
        view.pagingEnabled = true
        
        //设置弹簧效果
        view.bounces = false
        
        //设置滚动条
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        
        //设置代理 监听滚动
        view.delegate = self
        
        return view
        
    }()
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: - 监听pageView的滚动
extension WBEmoticonKeyBoardView:UICollectionViewDelegate{
    
    //滚动的时候会走这个方法
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //获取屏幕中心点 相对于collectionview
        let centerX = self.pageView.contentOffset.x + ScreenW * 0.5
        
        //获取当前显示的cell //对数组中的cell进行排序
        let cells = self.pageView.visibleCells().sort { (cell1, cell2) -> Bool in
            return cell1.frame.origin.x < cell2.frame.origin.y
        }
        
        if cells.count == 2{
            
            //获取第一个cell
            let firstCell = cells.first!
            
            //获取第二个cell
            let lastCell = cells.last!
            let indexPath: NSIndexPath
            //判断中心点在那个cell上面
            if firstCell.frame.contains(CGPoint(x: centerX, y: 0)) {
                
                //说明中心点在第一个cell上面 第一个cell显示的多
                indexPath = pageView.indexPathForCell(firstCell)!
                
            }else{
                
                indexPath = pageView.indexPathForCell(lastCell)!
                
            }
            
            bottomView.setupCurrentButton(indexPath.section)
            
            //设置pageControl
            setupPageControl(indexPath)
            
        }
        
    }
    
}

// MARK: - 设置pageControl
extension WBEmoticonKeyBoardView{

    func setupPageControl(indexPath:NSIndexPath){
        
        self.pageControl.numberOfPages = WBEmoticonTool.sharedEmoticonTool.allEmoticons[indexPath.section].count
        
        self.pageControl.currentPage = indexPath.item
        
    }

}



